package com.xxg.socket.impl;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * byte[] 编码器
 */
public class ByteArrayEncoder extends MessageToByteEncoder {

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        if (msg instanceof byte[]) {
            byte[] data = (byte[]) msg;
            out.writeInt(data.length);
            out.writeBytes(data);
        }
    }
}
